/* 变量 */
var vote = "other/vote.html";

// 投票
var contactsCallback_vote = myApp.onPageInit('vote', function (page) {

    if (!hasAccount() || !hasOpenId()) {
        $$("#vote_page .novote").show();
        return;
    }

    //处理参数
    var account = iss_localStorage.get("yuqing_appAccount"); // 全局变量
    var openid = iss_localStorage.get("yuqing_appOpenId"); // 全局变量

    var activityGuid = "";
    var isradio = "";

    var isPost = true;

    // 提交
    $$("#btnSubmit").click(function () {
        CheckPopLogin(function () {
            //提交操作
            var selectItemid = "";

            if (isradio == "true") {
                var list = $$('#vote_page input[type="radio"][checked="checked"]').val();
                if (list == undefined || list == null) {
                    alert("请选中一个!");
                    return false;
                }
                else {
                    selectItemid = list;
                }

            }
            else {
                $$('#vote_page input[type="checkbox"]').each(function () {
                    if (this.checked) {
                        selectItemid += $(this).val() + ',';
                    }
                });
                if (selectItemid == "") {
                    alert("请选中一个!");
                    return;
                }
                if (selectItemid.length > 0) {
                    selectItemid = selectItemid.substring(0, selectItemid.length - 1);
                }
            }

            if (isPost) {
                isPost = false;
                $$.ajax({
                    url: ISS_Constants.Url.const_weixin_vote,
                    data: { "action": "2", "conditionParm": "{'AccountGuid':'" + account + "','OpenId':'" + openid + "','ActivityGuid':'" + activityGuid + "','ActivityVoteGuid':'" + selectItemid + "'}" },
                    crossDomain: true,
                    dataType: "json",
                    success: function (data, status) {
                        isPost = true;

                        LoadData();
                    },
                    error: function (xhr, status) {
                        console.log("ajax error!");
                    }
                }); // end ajax
            }
        });
    }); // end submit

    // 初始化数据
    LoadData();

    // 初始化数据
    function LoadData() {
        $$.ajax({
            url: ISS_Constants.Url.const_weixin_vote,
            data: { "action": "1", "conditionParm": "{'AccountGuid':'" + account + "','OpenId':'" + openid + "'}" },
            crossDomain: true,
            dataType: "json",
            success: function (data, status) {
                if (data && data.success && data.datas) {
                    var datas = data.datas;

                    activityGuid = datas.Activity.ActivityGUID;
                    var votePerson = datas.VotePerson;
                    var content = "<span style='color: #444444; font-size: 14px; font-weight: bold; line-height: 16px;background-color: #fcfcfc;'>";
                    content += datas.Activity.ActivityIntroduction;
                    content += "</span>";
                    var msg = "";
                    if (datas.Activity.ActivitySelectedNum != null && datas.Activity.ActivitySelectedNum > 1) {
                        msg = "多选投票，共有" + votePerson + "人参与投票"
                    }
                    else {
                        msg = "单选投票，共有" + votePerson + "人参与投票";
                        isradio = "true";
                    }
                    $$("#vote_page #title").html(datas.Activity.ActivityName);
                    $$("#vote_page .date").html(datas.Activity.ActivityBeginTime.split("T")[0]);
                    $$("#vote_page .content").html(content);
                    $$("#vote_page .modus").html(msg);

                    if (!datas.IsVote) { //未投票
                        // 单选 or 多选
                        var list = "";
                        if (datas.ActivityVotes && datas.ActivityVotes != null && datas.ActivityVotes.length > 0) {
                            for (var i = 0; i < datas.ActivityVotes.length; i++) {

                                if (isradio == "true") {
                                    var li = "<li>";
                                    li += "<label class='label-checkbox item-content'>";
                                    li += "<input type='radio' name='my-radio' value='" + datas.ActivityVotes[i].ActivityVoteGUID + "'>";
                                    li += "<div class='item-media'><i class='icon icon-form-checkbox'></i></div>";
                                    li += "<div class='item-inner'><div class='item-title'>" + datas.ActivityVotes[i].ActivityVoteContent + "</div></div>";
                                    li += "</label>";
                                    li += "</li>";

                                    list += li;
                                }
                                else {
                                    var li = "<li>";
                                    li += "<label class='label-checkbox item-content'>";
                                    li += "<input type='checkbox' name='my-checkbox' value='" + datas.ActivityVotes[i].ActivityVoteGUID + "'>";
                                    li += "<div class='item-media'><i class='icon icon-form-checkbox'></i></div>";
                                    li += "<div class='item-inner'><div class='item-title'>" + datas.ActivityVotes[i].ActivityVoteContent + "</div></div>";
                                    li += "</label>";
                                    li += "</li>";

                                    list += li;
                                }
                            }
                            $$("#vote_page .list-block ul").html(list);

                            if (isradio == "true") {
                                // 列表点击效果
                                $$.each($$("#vote_page .list-block ul li"), function (n, n_value) {
                                    $$(n_value).click(function () {
                                        $$.each($$("#vote_page .list-block ul li"), function (m, m_value) {
                                            $$(m_value).find("input").attr('checked', '');
                                        });
                                        $$(this).find("input").attr('checked', 'checked');
                                    });
                                });
                            }
                            else {
                                // 列表点击效果
                                $$.each($$("#vote_page .list-block ul li"), function (n, n_value) {
                                    $$(n_value).click(function () {
                                        var status = $$(this).find("input").prop('checked');
                                        $$(this).find("input").prop('checked', !status);
                                    });
                                });
                            }
                        }
                        // 控制
                        $$("#vote_page .votecontent").show();
                        $$("#vote_page #btnSubmit").show();
                        $$("#vote_page .list-block").show();
                        $$("#vote_page .vote-result").hide();

                    } else { //已投票
                        var color = ["pink", "yellow", "orange", "blue", "green"];
                        var list = "";
                        if (datas.ActivityVotesTj && datas.ActivityVotesTj != null && datas.ActivityVotesTj.length > 0) {
                            for (var i = 0; i < datas.ActivityVotesTj.length; i++) {
                                var index = i % 5;
                                var percent = parseInt(parseInt(datas.ActivityVotesTj[i].Value) / parseInt(datas.VoteRecord) * 100);

                                var li = "<li>";
                                li += "<h5>" + datas.ActivityVotesTj[i].Key + "</h5>";
                                li += "<div class='pbg'><div style='width: " + percent + "%;' class='pbr bg-" + color[index] + "'></div></div>";
                                li += "<span class='percentage'>" + datas.ActivityVotesTj[i].Value + "(" + percent + "%)</span>";
                                li += "</li>";

                                list += li;
                            }
                            $$("#vote_page .vote-result ul").html(list);
                        }
                        // 控制
                        $$("#vote_page .votecontent").show();
                        $$("#vote_page #btnSubmit").hide();
                        $$("#vote_page .list-block").hide();
                        $$("#vote_page .vote-result").show();
                    }
                } else {  /* alert("没有活动信息"); */$$("#vote_page .novote").show(); }
            },
            error: function (xhr, status) {
                $$("#vote_page .novote").show();
                console.log("vote ajax error!");
            }
        });
    }; // end LoadDate
});


function initData() {
    var href = window.location.href;
    if (href.indexOf(vote) > -1) {
        contactsCallback_vote.trigger();
    }
}

initData();